package com.boolib.animate.framework.service.advice;

import com.alibaba.fastjson.JSON;
import com.boolib.animate.commons.Consts;
import com.boolib.animate.framework.entity.*;

import com.boolib.animate.framework.entity.bo.AnimateWorkBo;
import com.boolib.animate.framework.mapper.AnimateSearchTagMapper;
import com.boolib.animate.framework.mapper.AnimateSearchWorkTagMapper;
import com.boolib.animate.framework.mapper.define.QueryAnimateWorkMapper;
import com.boolib.animate.framework.service.base.impl.BaseAnimateChapterCommentService;
import com.boolib.animate.framework.service.base.impl.BaseAnimateChapterService;
import com.boolib.animate.framework.service.base.impl.BaseAnimateWorkService;
import com.boolib.animate.framework.service.base.impl.FileInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author nier
 * @create 2020-02-20 16:20
 */
@Slf4j
@Service
public class AnimateWorkService {

    @Value("${system.uploadpath}")
    private String uploadPath;

    @Autowired
    private FileInfoService fileInfoService;

    @Autowired
    private BaseAnimateWorkService baseAnimateWorkService;

    @Autowired
    private AnimateSearchTagMapper animateSearchTagMapper;

    @Autowired
    private AnimateSearchWorkTagMapper animateSearchWorkTagMapper;

    @Autowired
    private QueryAnimateWorkMapper queryAnimateWorkMapper;

    @Autowired
    private BaseAnimateChapterService baseAnimateChapterService;

    @Autowired
    private BaseAnimateChapterCommentService baseAnimateChapterCommentService;


    @Transactional
    public AnimateWork newAnimateWork(AnimateWork animateWork) throws IOException, FileUploadException {
        animateWork.setCoverImage(fileInfoService.uploadImage(uploadPath, animateWork.getPageFile(), FileInfo.FILETYPE_PAGE));;
        return   baseAnimateWorkService.saveSelective(animateWork);
    }


    public PageInfo<AnimateWork> queryWork(Example example, Integer page, Integer size) {
        Example example1 = new Example(AnimateSearchTag.class);
        example1.createCriteria().andEqualTo("showStatus",Consts.ENABLE).andEqualTo("isDel",Consts.ENABLE);
        final List<AnimateSearchTag> animateSearchTags = animateSearchTagMapper.selectByExample(example1);
        Map map = new HashMap<>();
        animateSearchTags.forEach(item->{
            map.put(item.getId(),item);
        });

        PageHelper.startPage(page,size);

        final List<AnimateWork> animateWorks = baseAnimateWorkService.queryByExample(example);

        PageInfo p =new PageInfo(animateWorks);
        animateWorks.forEach(item->{
            Example example2 = new Example(AnimateSearchWorkTag.class);
            example2.createCriteria().andEqualTo("workId",item.getId()).andEqualTo("showStatus",Consts.ENABLE).andEqualTo("isDel",Consts.ENABLE);
            final List<AnimateSearchWorkTag> animateSearchWorkTags = animateSearchWorkTagMapper.selectByExample(example2);
            List list=new ArrayList();
            animateSearchWorkTags.forEach(x->{
                list.add(map.get(x.getTagId()));
            });
            item.setTagList(list);
        });


        return p ;
    }


    public PageInfo<QueryWork> queryWorkTag(Integer tag, Integer location, Integer update, Integer face, Integer sort, Integer page, Integer size) {

        PageHelper.startPage(page,size);
        return new PageInfo<QueryWork>(queryAnimateWorkMapper.queryWorkTag(tag,location,update,face,sort));

    }

    public AnimateWork queryChapterList(Long id,   String sortType) {

        final AnimateWork animateWork = baseAnimateWorkService.queryById(id);

        Example animateSearchTagExample = new Example(AnimateSearchTag.class);

        animateSearchTagExample.createCriteria().andEqualTo("showStatus",Consts.ENABLE).andEqualTo("isDel",Consts.ENABLE);

        final List<AnimateSearchTag> animateSearchTags = animateSearchTagMapper.selectByExample(animateSearchTagExample);

        Map<Long,AnimateSearchTag> map = new HashMap<Long,AnimateSearchTag>();

        animateSearchTags.forEach(item->{
            map.put(item.getId(),item);
        });

        Example animateSearchWorkTagExample = new Example(AnimateSearchWorkTag.class);

        animateSearchWorkTagExample.createCriteria().andEqualTo("workId",animateWork.getId()).andEqualTo("showStatus",Consts.ENABLE).andEqualTo("isDel",Consts.ENABLE);

        final List<AnimateSearchWorkTag> animateSearchWorkTags = animateSearchWorkTagMapper.selectByExample(animateSearchWorkTagExample);

        List <AnimateSearchTag> list = new ArrayList<>();

        animateSearchWorkTags.forEach(x->{
            list.add(map.get(x.getTagId()));
        });
        animateWork.setTagList(list);


        Example example = new Example(AnimateChapter.class);


        if("desc".equals(sortType)) {
            example.orderBy("idxOrder").desc();
        }
        else{
            example.orderBy("idxOrder").asc();
        }
        final List<AnimateChapter> animateChapters = baseAnimateChapterService.queryByExample(example);

        animateWork.setChapterList(animateChapters);

        Example example1 = new Example(AnimateChapterComment.class);


        example1.createCriteria().andEqualTo("workId",animateWork.getId()).andEqualTo("isDel",Consts.ENABLE).andEqualTo("showStatus",Consts.ENABLE);

        example1.orderBy("createTime").desc();

        final List<AnimateChapterComment> animateChapterComments = baseAnimateChapterCommentService.queryByExample(example1);

        animateWork.setCommentList(animateChapterComments);

        return animateWork;

    }
}
